
package com.huifan.demo.util;

import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class TranslationTool {
    public TranslationTool() {
    }

    public static String getStrFromBASE64(String s) {
        if (s == null)
            return null;
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(s);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }

    public static String getStrFromBASE64(byte[] s) {
        if (s == null)
            return null;
        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        try {
            return encoder.encode(s);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * base64解码
     */
    public static byte[] getbyteFromBASE64(String s) {
        if (s == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            return decoder.decodeBuffer(s);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] shortToByte(short pShort) {
        byte[] bb = new byte[]{(byte) ((pShort & '\uff00') >>> 8), (byte) (pShort & 255)};
        return bb;
    }

    public static byte[] intToByte(int pInt) {
        byte[] bi = new byte[]{(byte) ((pInt & -16777216) >>> 24), (byte) ((pInt & 16711680) >>> 16), 6, 5};
        bi[2] = (byte) ((pInt & '\uff00') >>> 8);
        bi[3] = (byte) (pInt & 255);
        return bi;
    }

    public static int byteToInt(byte[] pByte, int pbegin, int len) {
        int ret = 0;

        for (int i = pbegin; i < pbegin + len; ++i) {
            ret = ret << 8 & -256 | pByte[i] & 255;
        }

        return ret;
    }

    public static short byteToShort(byte[] pByte, int pbegin, int len) {
        short ret = 0;

        for (int i = pbegin; i < pbegin + len; ++i) {
            ret = (short) ((short) (ret << 8) & '\uff00' | (short) (pByte[i] & 255));
        }

        return ret;
    }

    public static String byteToString(byte[] pByte, int pbegin, int len) throws UnsupportedEncodingException {
        return new String(pByte, pbegin, len, "gb2312");
    }

    public static String HextoASCII(byte[] byteSrc, int start, int length) {
        if (start >= 0 && length >= 0) {
            if (start + length > byteSrc.length) {
                throw new ArrayIndexOutOfBoundsException();
            } else {
                StringBuffer hexTableBuffer = new StringBuffer(2 * length);

                for (int i = start; i < start + length; ++i) {
                    String num = Integer.toHexString(byteSrc[i] & 255).toUpperCase();
                    if (num.length() < 2) {
                        hexTableBuffer.append('0');
                    }

                    hexTableBuffer.append(num);
                }

                return hexTableBuffer.toString();
            }
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    public static String HextoASCII(byte[] byteSrc) {
        return HextoASCII(byteSrc, 0, byteSrc.length);
    }

    public static byte[] ASCIItoHex(String asciistring) {
        if (asciistring.length() % 2 != 0) {
            throw new IllegalArgumentException("string length error");
        } else {
            byte[] coverdata = new byte[asciistring.length() / 2];

            for (int i = 0; i < coverdata.length; ++i) {
                coverdata[i] = (byte) Integer.parseInt(asciistring.substring(i * 2, (i + 1) * 2), 16);
            }

            return coverdata;
        }
    }

    public static byte[] readFile(String pathandname) throws IOException {
        InputStream fs = TranslationTool.class.getClassLoader().getResourceAsStream(pathandname);
        byte[] temp = new byte[fs.available()];
        fs.read(temp);
        fs.close();
        return temp;
    }

    public static byte[] readFile(String pathandname, int start) throws IOException {
        InputStream fs = TranslationTool.class.getClassLoader().getResourceAsStream(pathandname);
        if (start >= 0 && start <= fs.available()) {
            byte[] temp = new byte[fs.available() - start];
            fs.skip((long) start);
            fs.read(temp);
            fs.close();
            return temp;
        } else {
            throw new IOException();
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String test = "C466E6F746";
        byte[] a = test.getBytes();
        a = ASCIItoHex(test);
        System.out.println(new String(a, "gb2312"));
    }
}
